﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Linq;

namespace Perpus
{
    public partial class TampilanAnggota : Form
    {
        public TampilanAnggota()
        {
            InitializeComponent();
        }

        string konek = MappingDatabase.koneksi;

        private ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(konek))
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<AnggotaMap>())
                .BuildSessionFactory();
        }

        private void TampilanAnggota_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dotnetDataSet.Anggota' table. You can move, or remove it, as needed.
            this.anggotaTableAdapter.Fill(this.dotnetDataSet.Anggota);
            // TODO: This line of code loads data into the 'cobaDataSetBaru.Anggota' table. You can move, or remove it, as needed.
            
            

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            admin_tambahAnggota tambah_anggota = new admin_tambahAnggota();
            tambah_anggota.Show();
            this.Hide();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            admin_MenuAwal menu = new admin_MenuAwal();
            menu.Show();
            this.Hide();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;
            textBox1.Text = dataGridView1[0, i].Value.ToString();
            textBox2.Text = dataGridView1[1, i].Value.ToString();
            comboBox1.Text = dataGridView1[4, i].Value.ToString();
            textBox3.Text = dataGridView1[2, i].Value.ToString();
            textBox4.Text = dataGridView1[3, i].Value.ToString();
            //textBox1.Text = dataGridView1.
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var factory = CreateSessionFactory();

            int id = Convert.ToInt32(textBox1.Text);

            using (var session = factory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var idAnggota = session.Get<Anggota>(id);
                    idAnggota.nama_anggota = textBox2.Text;
                    idAnggota.alamat_anggota = textBox3.Text;
                    idAnggota.no_telp = textBox4.Text;
                    idAnggota.gender_anggota = comboBox1.SelectedItem.ToString();

                    transaction.Commit();

                }
                MessageBox.Show("Berhasil!");
            }   
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var factory = CreateSessionFactory();

            int noreg = Convert.ToInt32(textBox1.Text);

            try
            {
                using (var session = factory.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {

                        var member = new Anggota
                        {
                           nomor_registrasi = noreg,
                           nama_anggota = textBox2.Text,
                           gender_anggota = comboBox1.SelectedValue.ToString(),
                           no_telp = textBox4.Text,
                           alamat_anggota = textBox3.Text

                        };


                        session.Delete(member);

                        transaction.Commit();
                        MessageBox.Show("Data Berhasil Dihapus!");
                        dataGridView1.DataSource = dotnetDataSet.Buku;
                    }
                }

            }
            catch
            {
                MessageBox.Show("Gagal!");
            }
        }

       
    }
}
